﻿@using PerpetuumSoft.Knockout
@using TimeProject.WebSite.Views;

@model TimeProject.WebSite.ViewModels.UsersViewModel
@{
    var ko = Html.CreateKnockoutContext();
}

@using (ko.With(m => m.Messages))
{ @Html.GetInfoBox(Model.Messages) }




@*@using (ko.With(m => m.Errors)) { @Html.InfoBoxFor(Model) }*@

@*@using (ko.With(m => m.Errors))
    {
        @Html.Partial("_infoboxlauncher", Model.Errors)
    }*@

@*@using (var subModel = ko.With(m => m.Errors))
    {
        @Html.Partial("_infoboxlauncher", Model.Errors)
    }*@


<div class="page-header">
    <h1>USERS <small>View and manage user data.</small></h1>

    @*    @using (var subModel = ko.With(m => m.Errors))
        {
            using (subModel.If(x => x.Has1))
            {
                @Html.Partial("_infobox", Model.Errors)
            }
            using (subModel.If(x => x.Has2))
            {
                @Html.Partial("_infobox", Model.Errors)
            }
        }*@



</div>


@*@using (Html.BeginForm("SaveUserModel", "App", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal" }))
*@

<form class="form-horizontal">
    <table class="table table-striped table-hover table-condensed">
        <caption>Tämä on taulukon otsikko</caption>
        <thead>
            @{ var header = Model.Users.FirstOrDefault(); }
            <tr>
                <th style="width: 10%">@Html.DisplayNameFor(model => header.UserId)</th>
                <th style="width: 20%">@Html.DisplayNameFor(model => header.Username)</th>
                <th style="width: 20%">@Html.DisplayNameFor(model => header.FullName)</th>
                <th style="width: 20%">@Html.DisplayNameFor(model => header.EMail)</th>
                <th style="width: 10%">@Html.DisplayNameFor(model => header.StatusText)</th>
                <th style="width: 20%"></th>
            </tr>
        </thead>

        <tbody>
            @using (var lines = ko.Foreach(m => m.Users))
            {
                <tr>
                    <td>@lines.Html.Span(m => m.UserId)</td>
                    <td>@lines.Html.Span(m => m.Username)</td>
                    <td>@lines.Html.Span(m => m.FullName)</td>
                    <td>@lines.Html.Span(m => m.EMail)</td>
                    <td>@lines.Html.Span(m => m.StatusText)</td>
                    <td>
                        @using (lines.If(user => user.IsActive))
                        {
                            @lines.Html.HyperlinkButton("Passivate", "PassivateUser", "TimeProject", new { index = "$index()" })
                        }
                        @using (lines.If(user => user.IsActive == false))
                        {
                            @lines.Html.HyperlinkButton("Activate", "ActivateUser", "TimeProject", new { index = "$index()" })
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>
    @using (ko.If(model => model.Users.Count == 0))
    {
        <div class="table-no-items-text-container">
            <div class="table-no-items-text">No items available.</div>
        </div>
    }


    @ko.Html.GetButton("Add new user", "AddLine")
    @ko.Html.GetButtonRight("Save", "Save")



    @*@using (ko.If(x => x.Users.Count < 500))
    {
    }*@


    @*<form method="POST" action="">
        <input type="submit" value="Submit" name="submitBtn" onclick="this.disabled=true;this.form.submit();" >
        </form>*@
    @ko.Init(Model)
</form>

@*@using (Html.BeginForm("SaveXXX", "App", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal" }))
    {

        <div class="control-group">
            <div class="control-label">
            </div>
            <div class="controls">
                @Html.TextBoxFor(model => model.JokuArvo)
            </div>
        </div>

        <div class="control-group">
            <div class="control-label">
            </div>
            <div class="controls">

    @Html.DropDownListFor(model => model.TaskId, new SelectList(
                      new List<Object>{
                           new { value = 0 , text = "Red"  },
                           new { value = 1 , text = "Blue" },
                           new { value = 2 , text = "Green"}
                        },
                      "value",
                      "text",
                       2))
            </div>
        </div>


        <div class="control-group">
            <div class="control-label">
            </div>
            <div class="controls">
                @ko.Html.TextBox(m => m.JokuArvo, new { @type = "text" })
            </div>
        </div>

        <table class="table table-striped table-bordered table-hover table-condensed">
            <thead>
                @{ var header = Model.Users.FirstOrDefault(); }
                <tr>
                    <th>Project
                    </th>
                    <th>Task
                    </th>

                    <th>
                        @Html.DisplayNameFor(model => header.UserId)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => header.UserName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => header.FullName)
                    </th>
                </tr>
            </thead>

            <tbody>
                @using (var lines = ko.Foreach(m => m.Users))
                {
                    <tr>
                        <td>
                            @lines.Html.DropDownList(m => ko.Model.ProjectId, null, item => ko.Model.DataBase[item].Name).Value(m => m.ProjectId).OptionsCaption("Select ...")

                        </td>

                        <td>
                            @lines.Html.DropDownList(m => ko.Model.TaskId[m.ProjectId], null, (m, item) => ko.Model.DataBase[m.ProjectId].Tasks[item].Name).Value(m => m.TaskId).OptionsCaption("Select...")
                        </td>
                        <td>
                            @lines.Html.TextBox(m => m.UserId, new { @type = "text" }).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDown)
                        </td>

                        <td>
                            @lines.Html.TextBox(m => m.UserName, new { @type = "text" }).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDown)
                        </td>

                        <td>
                            @lines.Html.TextBox(m => m.FullName, new { @type = "text" }).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDown)
                        </td>
                    </tr>
                }
            </tbody>
        </table>

        @ko.Html.Button("Add product", "AddLine", "App")

        @ko.Apply(Model)
    }*@